//
//  SIXScrollView.m
//  FastDoctor_6
//
//  Created by cool on 16/3/1.
//  Copyright © 2016年 cool. All rights reserved.
//

#import "SIXScrollView.h"

@interface SIXScrollView()<UIScrollViewDelegate>
@property (nonatomic, strong) NSMutableArray *imagesArr;

@end

@implementation SIXScrollView

- (NSMutableArray *)imagesArr{
    if (nil == _imagesArr) {
        _imagesArr = [NSMutableArray array];
    }
    return _imagesArr;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubviews];
        self.delegate =self;
        
    }
    return self;
}

- (void)addSubviews
{
    
    for (int i = 0; i < 3; i++) {
       
        UIImageView *scImageView = [[UIImageView alloc] init];
        
   
        
        scImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"banner%d",i]];
       
        ///用户交互
       scImageView.userInteractionEnabled = YES;
        ///手势点击时间进行触发跳转页面
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scImageViewDidClick:)];
        
        [scImageView addGestureRecognizer:tap];
        
        [self.imagesArr addObject:scImageView];
        
        [self insertSubview:scImageView atIndex:0];

    }
    
    ///添加pageControl
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    
    self.pageControl = pageControl;
    
    pageControl.numberOfPages = self.imagesArr.count;
    
    pageControl.currentPage = 0;
    
    pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    
    
    
    [self addTimer];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat W = self.bounds.size.width;
    CGFloat H = self.bounds.size.height;
    
    for (int i = 0; i < self.imagesArr.count; i++) {
        UIImageView *scImageView = self.imagesArr[i];
        
        scImageView.frame =scImageView.frame = CGRectMake(i * W, 0, W, H);
    }
    
   
}

#pragma mark -
#pragma mark - 添加定时器
- (void)addTimer{
    
    self.timer =  [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    
    

}


- (void)nextImage{
    CGPoint contentOffset = self.contentOffset;
    
    contentOffset.x = self.pageCount * self.bounds.size.width;
    
    [self setContentOffset:contentOffset animated:YES];
    
    self.pageControl.currentPage = self.pageCount;
  
    if (self.pageCount == self.imagesArr.count - 1) {
    
        self.pageCount = 0;
        
        contentOffset.x = 0;
        
        return;
    
    }else {
        self.pageCount++;
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger pageCount = (offsetX + scrollView.frame.size.width * 0.5) / scrollView.frame.size.width ;
    self.pageControl.currentPage = pageCount;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    CGPoint contentOffset = scrollView.contentOffset;
    
    self.pageCount = contentOffset.x / self.bounds.size.width;
    
    self.pageControl.currentPage = self.pageCount;
    
    [self addTimer];
}



@end
